//
//  TLProjectCell.m
//  Coding_iOS
//
//  Created by taolei-mac on 2017/12/27.
//  Copyright © 2017年 com.lianchuan. All rights reserved.
//

#define kIconSize 80

#import "TLProjectCell.h"
#import "TLProjectInfo.h"

@interface TLProjectCell ()

@property (nonatomic, weak) UIImageView *projectIcon;

@property (nonatomic, weak) UILabel *projectName;

@property (nonatomic, weak) UILabel *authorLabel;

@property (nonatomic, weak) UILabel *descriptLabel;

@property (nonatomic, weak) UIImageView *privateIconView;

@end

@implementation TLProjectCell

- (void)setProjectInfo:(TLProjectInfo *)projectInfo
{
    _projectInfo = projectInfo;
    
    NSString *iconUrl = projectInfo.icon;
    if(![iconUrl containsString:@"http"]){
        iconUrl = [NSString stringWithFormat:@"%@%@", @"https://coding.net", iconUrl];
    }
    [_projectIcon sd_setImageWithURL:[NSURL URLWithString:iconUrl] placeholderImage:nil];
    
    _privateIconView.hidden = projectInfo.is_public;
    _privateIconView.frame = CGRectMake(CGRectGetMaxX(_projectIcon.frame) + 20, _projectIcon.y + 2, 15, 15);
    
    _projectName.text = projectInfo.name;
    [_projectName sizeToFit];
    CGFloat projectNameX = projectInfo.is_public ? 20 : 40;
    _projectName.x = CGRectGetMaxX(_projectIcon.frame) + projectNameX;
    _projectName.y = _projectIcon.y;
    
    _authorLabel.text = projectInfo.owner_user_name;
    [_authorLabel sizeToFit];
    _authorLabel.frame = CGRectMake(CGRectGetMaxX(_projectIcon.frame) + 20, CGRectGetMaxY(_projectIcon.frame) - 20, _authorLabel.width, 20);
    
    _descriptLabel.text = projectInfo.project_description;
    [_descriptLabel sizeToFit];
    [_descriptLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_projectIcon.mas_right).offset(20);
        make.centerY.offset(0);
        make.size.mas_equalTo(_descriptLabel.size);
    }];
}

+ (instancetype)createProjectCellWithTableView:(UITableView *)tableView
{
    static NSString *Id = @"project";
    TLProjectCell *cell = [tableView dequeueReusableCellWithIdentifier:Id];
    if(!cell){
        
        cell = [[TLProjectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Id];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        
        UIImageView *projectIcon = [[UIImageView alloc] init];
        projectIcon.frame = CGRectMake(15, 15, kIconSize, kIconSize);
        [self.contentView addSubview:projectIcon];
        self.projectIcon = projectIcon;
        
        UIImageView *privateIconView = [[UIImageView alloc] init];
        privateIconView.image = [UIImage originImageWithName:@"icon_project_private.png"];
        [self.contentView addSubview:privateIconView];
        self.privateIconView = privateIconView;
        
        UILabel *projectName = [[UILabel alloc] init];
        projectName.textColor = [UIColor colorWithHexString:@"0x323A45"];
        projectName.font = [UIFont systemFontOfSize:17];
        [self.contentView addSubview:projectName];
        self.projectName = projectName;
        
        UILabel *authorLabel = [[UILabel alloc] init];
        authorLabel.textColor = [UIColor colorWithHexString:@"0xA9B3BE"];
        authorLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:authorLabel];
        self.authorLabel = authorLabel;
        
        UILabel *descriptLabel = [[UILabel alloc] init];
        descriptLabel.textColor = [UIColor colorWithHexString:@"0x76808E"];
        descriptLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:descriptLabel];
        self.descriptLabel = descriptLabel;
        
        UIButton *setButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [setButton setImage:[UIImage originImageWithName:@"btn_setFrequent"] forState:UIControlStateNormal];
        [self.contentView addSubview:setButton];
        [setButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-10);
            make.bottom.offset(-5);
            make.size.mas_equalTo(CGSizeMake(35, 20));
        }];
    }
    return self;
}

@end
